/*
 * Copyright (c) 2023-2023 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.excel.converters;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * {@link LocalDateTime} 转换
 *
 * @author zeng
 */
public class LocalDateTimeStringConverter implements Converter<LocalDateTime> {

  private static final String MINUS = "-";

  @Override
  public Class<?> supportJavaTypeKey() {
    return LocalDateTime.class;
  }

  @Override
  public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
  }

  @Override
  public LocalDateTime convertToJavaData(
      ReadCellData cellData,
      ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) {
    String stringValue = cellData.getStringValue();
    String pattern =
        contentProperty == null || contentProperty.getDateTimeFormatProperty() == null
            ? switchDateFormat(stringValue)
            : contentProperty.getDateTimeFormatProperty().getFormat();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    return LocalDateTime.parse(cellData.getStringValue(), formatter);
  }

  @Override
  public WriteCellData<String> convertToExcelData(
      LocalDateTime value,
      ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) {
    String pattern =
        contentProperty == null || contentProperty.getDateTimeFormatProperty() == null
            ? DateUtils.DATE_FORMAT_19
            : contentProperty.getDateTimeFormatProperty().getFormat();

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    return new WriteCellData<>(value.format(formatter));
  }

  /**
   * switch date format
   *
   * @param dateString dateString
   * @return pattern
   */
  private static String switchDateFormat(String dateString) {
    int length = dateString.length();
    return switch (length) {
      case 19 ->
          dateString.contains(MINUS)
              ? DateUtils.DATE_FORMAT_19
              : DateUtils.DATE_FORMAT_19_FORWARD_SLASH;
      case 17 -> DateUtils.DATE_FORMAT_17;
      case 14 -> DateUtils.DATE_FORMAT_14;
      case 10 -> DateUtils.DATE_FORMAT_10;
      default -> throw new IllegalArgumentException("can not find date format for：" + dateString);
    };
  }
}
